package cn.luncene;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

import java.io.File;

/**
 * @author Administrator
 * @Title: 萧萧
 * @ProjectName lucene
 * @Description: 数值范围查询
 * @date 2018/9/29 19:55
 */
public class RangeQuery {
    @Test
    public void test() throws Exception{
        Directory directory = FSDirectory.open(new File("I:\\视频\\lucene学生\\02.参考资料\\index").toPath());
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);

        Query query = LongPoint.newRangeQuery("size",0L,1000L);
        //Query query = new TermQuery(new Term("size","82"));
        TopDocs topDocs = indexSearcher.search(query,10);
        System.out.println("记录数有: "+topDocs.totalHits);

        for(ScoreDoc scoreDoc:topDocs.scoreDocs){
            System.out.println("==========================================");
            Document document = indexSearcher.doc(scoreDoc.doc);
            System.out.println(document.get("content"));
            System.out.println(document.get("size"));
            System.out.println("========================================");
        }
        indexReader.close();
    }
}
